import { getUserInfo, authWeappUrl } from '@/common/dpapi/login';
import { loadUserInfo } from "@/common/apis/api/user";

const mutations = {
  SET_USER_INFO: (state, userInfo) => {
      uni.setStorageSync('user', userInfo)
    },
  SET_MYUSER_INFO: (state, userInfo) => {
      uni.setStorageSync('myuser', userInfo)
    },
}
const actions = {
  /**
   * 获取用户信息
   */
  userDetail({ commit }) {
    return new Promise((resolve, reject) => {
      getUserInfo({}).then(res => {
        if(res.payload) {
          const userInfo = res.payload
          const token = uni.getStorageSync('dp_token')
          userInfo.token = token
          commit('SET_USER_INFO', userInfo)
        }
        resolve(res)
      })
    })
  },
  /**
   * 获取自己接口用户信息
   */
  myUserDetail({ commit }) {
    return new Promise((resolve, reject) => {
      loadUserInfo().then(res => {
        if (res) {
          const userInfo = res.data;
          commit('SET_MYUSER_INFO',userInfo)
        }
        resolve(res)
      })
    })
  },


  // 微信登录, 获取openid
  loginByWeapp({ commit }, params) {
    return new Promise(async (resolve, reject) => {
      const url = await authWeappUrl(params)
			console.log(url,'url.......................')
      uni.request({
        url: url,
        data: params,
        header: {
          'content-type': 'application/json',
        },
        success(res) {
          resolve(res.data.payload)
        },
        fail(res) {
          reject(res)
        },
      })
    })
  }
}

export default {
    namespaced: true,
    mutations,
    actions
}